$(function () {

    initLayerDate("#order_begTime",false);
    initLayerDate("#order_endTime",false);
    //---------加载时间---开始
    initIndexTime();
    //---------加载时间---结束

    //加载显示基本数据统计
    salesBaseCount(function (data) {
        var userCountMap = data.userCountMap;
        var buyUserNum = userCountMap.buyUserNum;
        $("#buyUserNum").text(buyUserNum);
        var orderCountNum = data.orderCountNum;
        $("#orderCountNum").text(orderCountNum);
        var orderSumPrice = data.orderSumPrice;
        $("#sumOrderPrice").text(orderSumPrice);
    });

    /**
     * 获取基本数据统计
     * @param callback
     */
    function salesBaseCount(callback) {
        $.ajax({
            url:baseloaction +'/admin/count/salesBaseCount',
            type:'post',
            dataType:'json',
            success:function (result) {
                if(result.success==true){
                    callback(result.entity);
                }
            }
        });
    }



$("body").on('click','.order',function(){
        var params = $('#order-form').serializeJSON();
        var number = judgeState("order-form");
        if(number==1){
            queryOrderDayTimeNum(queryOrderDayTimeNumCallback,"");
        }else if(number==2){
            queryOrderDayTimeNum(queryOrderDayTimeNumCallback,params.begTime);
        }else  if(number==3){
            queryOrderDayNum(queryOrderDayTimeNumCallback);
        }
    });

    /**
     * 判断搜索状态
     * @param id
     * @returns {number}
     */
    function judgeState (id){
        var params = $('#'+id).serializeJSON();
        if(params.begTime==""&&params.endTime==""){
            return 1;
        }
        var oDate1 = new Date(params.begTime);
        var oDate2 = new Date(params.endTime);
        if(oDate1.getTime() == oDate2.getTime()){
            return 2;
        } else {
            return 3;
        }

    }

    //今日网站订单量曲线-----------开始
    /**
     * 今日下单与支付曲线
     * @param callback
     */
    //今日网站订单量曲线-----------开始
    queryOrderDayTimeNum(queryOrderDayTimeNumCallback,"");
    /**
     * 加载今日网站订单量统计数据
     * @param callback
     */
    function  queryOrderDayTimeNum(callback,date) {
        $.ajax({
            url:baseloaction +'/admin/count/queryOrderNowDayNum',
            type:'post',
            dataType:'json',
            data:{"date":date},
            success:function (result) {
                if(result.success==true){
                    callback(result,1);
                }
            }
        });
    }
    /**
     * 加载某时间端段的订单曲线
     * @param callback
     */
    function queryOrderDayNum(callback) {
        var params = $('#order-form').serializeJSON();
        params = JSON.stringify(params);
        $.ajax({
            url:baseloaction +'/admin/count/queryOrderDayNum',
            type:'post',
            data:params,
            dataType:'json',
            contentType:'application/json;charset=utf-8',
            success:function (result) {
                if(result.success==true){
                    callback(result,2);
                }
            }
        });
    }


    //显示数据统计
    function queryOrderDayTimeNumCallback(result,type){
        var yArray = new Array();
      var hoursArray =  ['0:00','1:00','2:00','3:00','4:00','5:00','6:00','7:00','8:00','9:00','10:00','11:00'
            ,'12:00','13:00','14:00','15:00','16:00','17:00','18:00','19:00','20:00','21:00','22:00','23:00']
        var max = result.max;
        var list = result.entity;
        var orderDaySumNumArr = new Array();
        var orderDaySuccessNumArr = new Array();
        if(type==1){
            yArray =hoursArray;
        }
        for(var i=0;i<list.length;i++){
            orderDaySumNumArr.push(list[i].sumNum);
            orderDaySuccessNumArr.push(list[i].successNum);
            if(type==2){
                yArray.push(list[i].date)
            }
        }
        var lineChart = echarts.init(document.getElementById("order-echarts-line-chart")),
            colors = ['#62A1FE', '#4ECE39', '#675bba'];
        var lineoption = {
            color: colors,
            tooltip: {
                trigger: 'axis',
                axisPointer: {
                    type: 'cross',
                    crossStyle: {
                        color: '#999'
                    }
                }
            },
            toolbox: {
                feature: {
                    dataView: {show: true, readOnly: false},
                    magicType: {show: true, type: ['line', 'bar']},
                    restore: {show: true},
                    saveAsImage: {show: true}
                }
            },
            legend: {
                data:['下单量','支付数']
            },
            xAxis: [
                {
                    type: 'category',
                    data: yArray,
                    axisPointer: {
                        type: 'shadow'
                    }
                }
            ],
            yAxis: [
                {
                    type: 'value',
                    name: '订单量',
                    min: 0,
                    max: max,
                    interval: 50,
                    axisLabel: {
                        formatter: '{value} 个'
                    }
                }
            ],
            series: [
                {
                    name:'下单量',
                    type:'line',
                    data:orderDaySumNumArr
                },
                {
                    name:'支付数',
                    type:'line',
                    data:orderDaySuccessNumArr
                }
            ]
        };
        lineChart.setOption(lineoption);
        $(window).resize(lineChart.resize);
    }
    //今日网站订单量曲线-----------结束

    //不同专业项目销转对比数据------开始

    /**
     * 不同专业项目销转对比数据
     * @param callback
     */
    function queryOrderSubjectPriceCountNum(callback) {
        $.ajax({
            url:baseloaction +'/admin/count/queryOrderSubjectPriceCountNum',
            type:'post',
            dataType:'json',
            success:function (result) {
                if(result.success==true){
                    callback(result.entity);
                }
            }
        });
    }

    /**
     * 显示数据
     */
    queryOrderSubjectPriceCountNum(function (dataList) {
        if(dataList!=null){
            var price = 0;
            var nameList = new Array();
            var priceList = new Array();
            for(var i=0;i<dataList.length;i++){
                var obj = dataList[i];
                price += parseFloat(obj.price);
                priceList.push(obj.price);
                var subjectList = obj.subjectList;
                var subjectNames = '';
                $.each(subjectList,function (index,subject) {
                    if(index < subjectList.length -1){
                        subjectNames +=subject.depict + '/';
                    }else{
                        subjectNames +=subject.depict;
                    }
                });
                nameList.push(subjectNames);
            }
            priceList.push(price);
            nameList.push("专业项目销售总金额(￥)");
            var orderSubjectPrice = echarts.init(document.getElementById("subject-order-echarts-line-chart")),
                colors = ['#62A1FE', '#4ECE39', '#675bba'];
            var lineoption = {
                title: {
                    text: '专业项目销售对比(只包含课程[套餐除外]、图书、试卷)',
                    subtext: ''
                },
                tooltip: {
                    trigger: 'axis',
                    axisPointer: {
                        type: 'shadow'
                    }
                },
                legend: {
                    data: ['销售金额']
                },
                grid: {
                    left: '3%',
                    right: '4%',
                    bottom: '3%',
                    containLabel: true
                },
                xAxis: {
                    type: 'value',
                    boundaryGap: [0, 0.01]
                },
                yAxis: {
                    type: 'category',
                    data: nameList
                },
                series: [
                    {
                        name: '销售金额',
                        type: 'bar',
                        data: priceList
                    }
                ]
            };
            orderSubjectPrice.setOption(lineoption);
            $(window).resize(orderSubjectPrice.resize);
        }
    });
    //不同专业项目销转对比数据------结束

    //订单额度与支付成功曲线-------开始
    /**
     * 查询统计订单价格区域的数据
     * @returns {*}
     */
    function getOrderPriceCountNumData(callback) {
        $.ajax({
            url: baseloaction +'/admin/count/queryOrderPriceCountNum',
            type:'post',
            dataType:'json',
            async:false,
            success:function (result) {
                if(result.success==true){
                    callback(result);
                }
            }
        });
    }
    getOrderPriceCountNumData(function (result) {
        var max = result.max;
        var orderPriceData = result.entity;
        var orderPriceDataList = new Array();
        if(orderPriceData!=null){
            orderPriceDataList.push(orderPriceData.price0Num);
            orderPriceDataList.push(orderPriceData.price10Num);
            orderPriceDataList.push(orderPriceData.price10_50Num);
            orderPriceDataList.push(orderPriceData.price50_100Num);
            orderPriceDataList.push(orderPriceData.price100_300Num);
            orderPriceDataList.push(orderPriceData.price300_800Num);
            orderPriceDataList.push(orderPriceData.price800Num);
        }
        var lineChart = echarts.init(document.getElementById("order-price-echarts-line-chart")),
            colors = ['#62A1FE', '#4ECE39', '#675bba'];
        var lineoption = {
            color: colors,
            tooltip: {
                trigger: 'axis',
                axisPointer: {
                    type: 'cross',
                    crossStyle: {
                        color: '#999'
                    }
                }
            },
            toolbox: {
                feature: {
                    dataView: {show: true, readOnly: false},
                    magicType: {show: true, type: ['line', 'bar']},
                    restore: {show: true},
                    saveAsImage: {show: true}
                }
            },
            legend: {
                data:['订单量']
            },
            xAxis: [
                {
                    type: 'category',
                    data: ['0元','<10元','10-50.00元','50.00-100.00元','100.00-300.00元','300.00-800.00元','>800.00元'],
                    axisPointer: {
                        type: 'shadow'
                    }
                }
            ],
            yAxis: [
                {
                    type: 'value',
                    name: '订单量',
                    min: 0,
                    max: max,
                    interval: 50,
                    axisLabel: {
                        formatter: '{value} 个'
                    }
                }
            ],
            series: [
                {
                    name:'订单量',
                    type:'line',
                    data:orderPriceDataList
                }
            ]
        };
        lineChart.setOption(lineoption);
        $(window).resize(lineChart.resize);
    });

    //订单额度与支付成功曲线-------结束

    //支付方式占比----------------开始
    function getPayTypeCountData(callback) {
        $.ajax({
            url: baseloaction +'/admin/count/queryOrderPayTypeCountNum',
            type:'post',
            dataType:'json',
            async:false,
            success:function (result) {
                if(result.success==true){
                    callback(result);
                }
            }
        });
    }
    getPayTypeCountData(function (result) {
        var payTypeData = result.entity;
        if(payTypeData!=null){
            var typeObject = new Array();
            var alipayNum = {};
            alipayNum.value=payTypeData.alipayNum;
            alipayNum.name='支付宝';
            typeObject.push(alipayNum);

            var weixinNum = {};
            weixinNum.value=payTypeData.weixinNum;
            weixinNum.name='微信';
            typeObject.push(weixinNum);

            var accountNum = {};
            accountNum.value=payTypeData.accountNum;
            accountNum.name='账户';
            typeObject.push(accountNum);

            var yeepayNum = {};
            yeepayNum.value=payTypeData.yeepayNum;
            yeepayNum.name='易宝';
            typeObject.push(yeepayNum);

            var yzfNum = {};
            yzfNum.value=payTypeData.yzfNum;
            yzfNum.name='易支付';
            typeObject.push(yzfNum);

            var freeNum = {};
            freeNum.value=payTypeData.freeNum;
            freeNum.name='免费赠送';
            typeObject.push(freeNum);

            var courseCartNum = {};
            courseCartNum.value=payTypeData.courseCartNum;
            courseCartNum.name='课程卡';
            typeObject.push(courseCartNum);

            var iosNum = {};
            iosNum.value=payTypeData.iosNum;
            iosNum.name='IOS内购';
            typeObject.push(iosNum);

            var payChart = echarts.init(document.getElementById("echarts-pay-chart"));
            var payoption = {
                tooltip : {
                    trigger: 'item',
                    formatter: "{a} <br/>{b} : {c} ({d}%)"
                },
                legend: {
                    x : 'center',
                    y : 'left',
                    data:['支付宝','微信','账户','易宝','易支付',"免费赠送","课程卡","IOS内购"]
                },
                toolbox: {
                    show : false,
                    feature : {
                        mark : {show: true},
                        dataView : {show: true, readOnly: false},
                        magicType : {
                            show: true,
                            type: ['pie', 'funnel']
                        },
                        restore : {show: true},
                        saveAsImage : {show: true}
                    }
                },
                calculable : true,
                series : [
                    {
                        name:'支付方式占比',
                        type:'pie',
                        radius : [30, 110],
                        center : ['50%', '50%'],
                        roseType : 'area',
                        data:typeObject
                    }
                ]
            };
            payChart.setOption(payoption);
            $(window).resize(payChart.resize);
        }
    });
    //支付方式占比----------------结束

    //支付渠道对比----------------开始
    /**
     * 查询支付渠道对比统计
     */
    function getOrderPayTypeCountNumData(callback) {
        $.ajax({
            url: baseloaction +'/admin/count/queryOrderFormCountNum',
            type:'post',
            dataType:'json',
            async:false,
            success:function (result) {
                if(result.success==true){
                    callback(result.entity);
                }
            }
        });
    }
    getOrderPayTypeCountNumData(function (orderFormData) {
        if(orderFormData!=null){
            var orderFormDataList = new Array();
            var webNum = {};
            webNum.name="PC";
            webNum.value=orderFormData.webNum;
            orderFormDataList.push(webNum);

            var appNum = {};
            appNum.name="APP";
            appNum.value=orderFormData.appNum;
            orderFormDataList.push(appNum);

            var mobileNum = {};
            mobileNum.name="H5";
            mobileNum.value=orderFormData.mobileNum;
            orderFormDataList.push(mobileNum);

            var adminNum = {};
            adminNum.name="后台开通";
            adminNum.value=orderFormData.adminNum;
            orderFormDataList.push(adminNum);

            var courseCartNum = {};
            courseCartNum.name="课程卡";
            courseCartNum.value=orderFormData.courseCartNum;
            orderFormDataList.push(courseCartNum);

            var applyNum = {};
            applyNum.name="小程序";
            applyNum.value=orderFormData.applyNum;
            orderFormDataList.push(applyNum);

            var orderFormChart = echarts.init(document.getElementById("pay-from-echarts-pie-chart"));
            var orderfromoption = {
                title : {
                    text: '支付渠道对比',
                    subtext: '',
                    x:'center'
                },
                tooltip : {
                    trigger: 'item',
                    formatter: "{a} <br/>{b} : {c} ({d}%)"
                },
                legend: {
                    orient: 'vertical',
                    left: 'left',
                    data: ['后台开通','PC','APP','H5','课程卡','小程序']
                },
                series : [
                    {
                        name: '支付渠道对比',
                        type: 'pie',
                        radius : '55%',
                        center: ['50%', '60%'],
                        data:orderFormDataList,
                        itemStyle: {
                            emphasis: {
                                shadowBlur: 10,
                                shadowOffsetX: 0,
                                shadowColor: 'rgba(0, 0, 0, 0.5)'
                            }
                        }
                    }
                ]
            };
            orderFormChart.setOption(orderfromoption);
            $(window).resize(orderFormChart.resize);
        }
    });
    //支付渠道对比----------------结束
});